/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann Tübingen, Germany 
 * Niko Schenk Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package collectors.impl;

import java.io.PrintWriter;
import util.sorting.Multiset;
import util.statistics.StatisticsHandler;
import collectors.api.Collector1;
import util.Interval;
import saphre.core.Store;
import saphre.core.SuffixArray;

/**
 * Class which simulates lcp-interval tree traversal by implementing the
 * Collector interface.
 * Program to collect supermaximal repeats from a corpus.
 * 
 * A supermaximal repeat is a repeat (ngram with term frequency > 1)
 * which - when extended to the left or right by one token - loses the property
 * of being a repeat, i.e. any extension to the left or right results
 * in an ngram with term frequency = 1.
 * 
 * @author niko
 */
public class SuperMaximalsCollector implements Collector1 {

    Store store;
    SuffixArray sa;
    double D;
    PrintWriter pw = null;
    
    private int minLengthOfPhrase;
    private int maxLengthOfPhrase;
    private int minTermFrequency;
    private String startsWith;
    
    private static int supermaximalcount = 0;
    private static int nonsupermaximalcount = 0;
    
    /**
     * Collect supermaximal phrases.
     * @param store
     * @param sa
     * @param top
     * @param pw
     * @param args 
     */
    public SuperMaximalsCollector(Store store, SuffixArray sa, PrintWriter pw, int aMinLengthOfPhrase,
            int aMaxLengthOfPhrase, int aMinTermFrequency, String aStartsWith) {
        this.store = store;
        this.sa = sa;
        //D = (double) store.numDocs();
        // Subtract 2! The program uses two initial and one final sentinel.
        D = (double) store.numDocs() - 2;
        this.pw = pw;
        
        this.minLengthOfPhrase = aMinLengthOfPhrase;
        this.maxLengthOfPhrase = aMaxLengthOfPhrase;
        this.minTermFrequency = aMinTermFrequency;
        this.startsWith = aStartsWith;
    }

    /**
     * 
     * @param pa 
     */
    public void addPrefixArray(SuffixArray pa) {
    }

    /**
     * 
     * @param inter
     * @param parentLcp
     * @param lcp
     * @param depth 
     */
    @Override
    public void preAdd(Interval inter, int parentLcp, int lcp, int depth) {
        
    }

    /**
     * 
     * @param inter
     * @param lcp
     * @param depth
     * @param docDist
     * @param leftContext 
     */
    @Override
    public void add(Interval inter, int lcp, int depth, Multiset docDist,
            Multiset leftContext) {

        Multiset dd = inter.docDist(sa, store);
        int tf = inter.tf();
        int df = dd.size();

        // Limit on the length of phrase.
        if (lcp >= minLengthOfPhrase && 
                lcp <= maxLengthOfPhrase && 
                tf >= minTermFrequency) {
           
            int loc = sa.getSuftab()[inter.lb()];
            String ngram = store.toString(loc, (loc + lcp));

            int ngramsize = loc - (loc - lcp); // der Klammerinhalt ist überflüssig.
            int[] generalngram = new int[ngramsize];
            int ngramindex = 0;
            // Collect this ngram. Every array index is occupied by an individual token/character.
            for (int l = 0; l < ngramsize; l++) {
                generalngram[l] = sa.getText()[loc + ngramindex];
                ngramindex++;
            }

            boolean isSupermaximal = false;
            boolean leftVariable = false;
            int b = store.text()[sa.getSuftab()[inter.lb()] - 1];
            for (int i = inter.lb(); i <= inter.rb(); i++) {
                int oneBefore = store.text()[sa.getSuftab()[i] - 1];
                if (oneBefore != b) {
                    leftVariable = true;
                }
            }

            if (leftVariable) {
                isSupermaximal = StatisticsHandler.isSupermaximal(inter, sa, lcp);
                if (isSupermaximal) {
                    supermaximalcount++;
                } else {
                    nonsupermaximalcount++;
                }
            }
            
            if(isSupermaximal && ngram.startsWith(startsWith)) {
                String rval = "ngram:" + ngram  + "\tlength:" + ngramsize + "\ttf:" + tf + "\tdf:" + df + "\n";
                pw.print(rval);
                System.out.print(rval);
            }
        }
    }

    /**
     * 
     * @param inter
     * @param lcp
     * @param depth
     * @param docDist
     * @param leftContext 
     */
    @Override
    public void addTrivial(Interval inter, int lcp, int depth,
            Multiset docDist, Multiset leftContext) {
    }
    
    /**
     * 
     * @return 
     */
    public static int getSupermaximalcount() {
        return supermaximalcount;
    }

    /**
     * 
     * @return 
     */
    public static int getNonSupermaximalcount() {
        return nonsupermaximalcount;
    }
}